//
//  MineOrderDetailViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/10/27.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "MineOrderDetailViewController.h"
#import "LOGoodsTableViewCell.h"
#import "LDPushOrderViewController.h"
#import "MineAssessViewController.h"
#import "MineRefundViewController.h"
#import "RefundSuccessViewController.h"
#import "RefundingViewController.h"

@interface MineOrderDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tab;
    NSMutableArray *_goodsArr;
    NSDictionary *_dataDic;
}
@property (weak, nonatomic) IBOutlet UIView *TopView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalMoney;
@property (weak, nonatomic) IBOutlet UILabel *freightLabel;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *PhoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderStatus;
@end

@implementation MineOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"订单详情";
    _goodsArr = [NSMutableArray array];
    [self downloadData];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineOrderDetail) Parameters:@{@"orderId":self.orderId, @"type":self.type, @"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        _dataDic = responseObject[@"data"];
        if (_dataDic[@"goods"]) {
            _goodsArr = _dataDic[@"goods"];
        }
        if ([_dataDic[@"isPay"] boolValue]) {
            _statusLabel.text = @"买家已付款";
        }else{
            _statusLabel.text = @"买家未付款";
        }
        
        _totalMoney.text = [NSString stringWithFormat:@"订单金额(含运费):￥%@", _dataDic[@"realTotalMoney"]];
        
        if ([_dataDic[@"deliverMoney"]integerValue]>0) {
            _freightLabel.text = [NSString stringWithFormat:@"运费金额:%@元", _dataDic[@"deliverMoney"]];
        }else{
            _freightLabel.text = [NSString stringWithFormat:@"运费金额:(包邮)"];
        }
        
        _userName.text = [NSString stringWithFormat:@"收件人:%@", _dataDic[@"userName"]];
        _PhoneLabel.text = [NSString stringWithFormat:@"%@", _dataDic[@"userPhone"]];
        _addressLabel.text = [NSString stringWithFormat:@"收货地址:%@", _dataDic[@"userAddress"]];
        
        if ([_dataDic[@"orderStatus"] integerValue] == -2) {
            _orderStatus.text = @"订单状态:待付款";
        }else if ([_dataDic[@"orderStatus"] integerValue] == 0) {
            _orderStatus.text = @"订单状态:待发货";
        }else if ([_dataDic[@"orderStatus"] integerValue] == 1) {
            _orderStatus.text = @"订单状态:待收货";
        }else if ([_dataDic[@"orderStatus"] integerValue] == 2) {
            _orderStatus.text = @"订单状态:待评价";
        }else if ([_dataDic[@"orderStatus"] integerValue] == -1) {
            _orderStatus.text = @"订单状态:已取消";
        }else{
            _orderStatus.text = @"订单状态:退换货";
        }
        [self createTableView];
        [_tab reloadData];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createTableView
{
    _tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70) style:(UITableViewStylePlain)];
    _tab.delegate = self;
    _tab.dataSource = self;
    _tab.showsVerticalScrollIndicator = NO;
//    _tab.scrollEnabled = NO;
    _tab.tableHeaderView = _TopView;
    _tab.tableFooterView = [self createFoootView];
    [_tab registerNib:[UINib nibWithNibName:@"LOGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LOGoodsTableViewCell"];
    [self.view addSubview:_tab];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _goodsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LOGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOGoodsTableViewCell"];
    GoodsListModel *model = [[GoodsListModel alloc]initWithDictionary:_goodsArr[indexPath.row] error:nil];
    [cell cellConfigureModel:model];
    cell.SaleLabel.hidden = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (UIView *)createFoootView
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 8)];
    line.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [footView addSubview:line];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 23, 230, 20)];

    if ([_dataDic[@"isPay"] boolValue]) {
        if ([_dataDic[@"deliverMoney"]integerValue]>0) {
            label.text = [NSString stringWithFormat:@"已付款:￥%@",_dataDic[@"totalMoney"]];
        }else{
            label.text = [NSString stringWithFormat:@"已付款:￥%@",_dataDic[@"totalMoney"]];
        }
    }else{
        if ([_dataDic[@"deliverMoney"]integerValue]>0) {
            label.text = [NSString stringWithFormat:@"待付款:￥%@",_dataDic[@"needPay"]];
        }else{
            label.text = [NSString stringWithFormat:@"待付款:￥%@",_dataDic[@"needPay"]];
        }
    }
    label.font = [UIFont systemFontOfSize:14.0];
    label.textColor = [UIColor colorWithHex:0x333333];
    [footView addSubview:label];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-80, 20, 60, 24)];
    if([_dataDic[@"orderStatus"] integerValue] == -3){
        
        [btn setTitle:@"修改退货" forState:UIControlStateNormal];
    }else if ([_dataDic[@"orderStatus"] integerValue] == -2) {
        [btn setTitle:@"立即付款" forState:UIControlStateNormal];
    }else if ([_dataDic[@"orderStatus"] integerValue] == 0) {
        [btn setTitle:@"立即退货" forState:UIControlStateNormal];
    }else if ([_dataDic[@"orderStatus"] integerValue] == 1) {
        [btn setTitle:@"确认收货" forState:UIControlStateNormal];
    }else if ([_dataDic[@"orderStatus"] integerValue] == 2) {
        [btn setTitle:@"立即退货" forState:UIControlStateNormal];
    }else if ([_dataDic[@"orderStatus"] integerValue] == -1) {
        btn.hidden = YES;
    }else{
        btn.hidden = YES;
    }
    [btn setBackgroundColor:[UIColor redColor]];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    btn.layer.cornerRadius = 5;
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(btn.left-70, 20, 60, 24)];
    NSString *str1;
    if ([_dataDic[@"orderStatus"] integerValue] == -2) {
        btn1.hidden = YES;//待付款
    }else if ([_dataDic[@"orderStatus"] integerValue] == 0) {
        btn1.hidden = YES;//待发货
    }else if ([_dataDic[@"orderStatus"] integerValue] == 1) {
        str1 = @"立即退货";//待收货
    }else if ([_dataDic[@"orderStatus"] integerValue] == 2) {
        btn1.hidden = YES;//待评价
    }else if ([_dataDic[@"orderStatus"] integerValue] == -1) {
        btn1.hidden = YES;//已取消
    }else{
        btn1.hidden = YES;//退换货
    }
    [btn1 setTitle:str1 forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn1 addTarget:self action:@selector(Btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn1];
    btn1.layer.cornerRadius = 5;
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 58, WIDTH, HEIGHT)];
    bottomView.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [footView addSubview:bottomView];
    
    return footView;
}
- (void)editRefund:(NSInteger)tag
{
    [THWManager PostHttp:API(MineOrderRefund) Parameters:@{@"orderId":_dataDic[@"orderId"], @"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        
        NSDictionary *_data = responseObject[@"data"][@"refunds"];
        
        if ([_data[@"refundStatus"]integerValue]==-1 ) {
            //退款失败
            RefundSuccessViewController *success = [[RefundSuccessViewController alloc]init];
            success.data = responseObject[@"data"][@"refunds"];
            success.isSuc = NO;;
            [self.navigationController pushViewController:success animated:YES];
        }else if([_data[@"refundStatus"]integerValue]==0){
            //等待处理
            RefundingViewController *refund = [[RefundingViewController alloc]init];
            
            refund.data = responseObject[@"data"][@"refunds"];
            refund.reasonDic = responseObject[@"data"][@"datas"];
            [self.navigationController pushViewController:refund animated:YES];
        }else if ([_data[@"refundStatus"]integerValue]==1)
        {
            //等待退款
            RefundingViewController *refund = [[RefundingViewController alloc]init];
            
            refund.data = responseObject[@"data"][@"refunds"];
            refund.reasonDic = responseObject[@"data"][@"datas"];
            [self.navigationController pushViewController:refund animated:YES];
        }else{
            //退款成功
            RefundSuccessViewController *success = [[RefundSuccessViewController alloc]init];
            success.data = responseObject[@"data"][@"refunds"];
            success.isSuc = YES;
            [self.navigationController pushViewController:success animated:YES];
        }
        
    } andFalse:^(NSError *error) {
        
    }];
}
- (void)BtnClick:(UIButton *)btn
{
    if ([btn.titleLabel.text isEqualToString:@"修改退货"]){
        
        [self editRefund:btn.tag];
    }else if ([btn.titleLabel.text isEqualToString:@"立即付款"]) {
        LDPushOrderViewController *pay = [[LDPushOrderViewController alloc] init];

        pay.isLineOnOrder = YES;
        pay.lineId = _dataDic[@"lineId"];
        pay.money = _dataDic[@"needPay"];
        pay.orderNo = _dataDic[@"orderNo"];
        pay.type = @"order";
        [self.navigationController pushViewController:pay animated:YES];
    }else if ([btn.titleLabel.text isEqualToString:@"立即评价"]){
        MineAssessViewController *assess = [[MineAssessViewController alloc]init];
        assess.type = @"shop";
        assess.orderId = _dataDic[@"orderId"];
        [self.navigationController pushViewController:assess animated:YES];
    }else if ([btn.titleLabel.text isEqualToString:@"立即退货"]){
        [THWManager PostHttp:API(MineOrderRefund) Parameters:@{@"orderId":_dataDic[@"orderId"], @"userToken":[Utils getUserToken]} Success:^(id responseObject) {
            if ([TXUtilsString isBlankString:responseObject[@"data"][@"refunds"]]) {
                MineRefundViewController *refund = [[MineRefundViewController alloc] init];
                refund.reasonDic = responseObject[@"data"][@"datas"];
                refund.moneyStr = responseObject[@"data"][@"totalMoney"];
                refund.orderId = responseObject[@"data"][@"orderId"];
                [self.navigationController pushViewController:refund animated:YES];
            }else{
                
            }
        } andFalse:^(NSError *error) {
            
        }];
    }
}

- (void)Btn1Click:(UIButton *)btn
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
